﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using StreetlightGames.SpaceShooter.Patterns;
using Microsoft.Xna.Framework;
using System.IO;
using System.Xml;

namespace StreetlightGames.SpaceShooter
{
    [DataContract]
    public class ShipData
    {
        /// <summary>Gets or sets the ShipType value</summary>
        [DataMember]
        public string ShipType
        {
            get { return _shipType; }
            set { _shipType = value; }
        }
        private string _shipType;

        /// <summary>Gets or sets the StartPosition value</summary>
        [DataMember]
        public string StartPosition
        {
            get { return _startPosition; }
            set { _startPosition = value; }
        }
        private string _startPosition;

        /// <summary>Gets or sets the StartDelay value</summary>
        [DataMember]
        public string StartDelay
        {
            get { return _startDelay; }
            set { _startDelay = value; }
        }
        private string _startDelay;

        /// <summary>Gets or sets the StartVelocity value</summary>
        [DataMember]
        public string StartVelocity
        {
            get { return _startVelocity; }
            set { _startVelocity = value; }
        }
        private string _startVelocity;

        /// <summary>Gets the Patterns value</summary>
        [DataMember]
        public List<PatternData> Patterns
        {
            get { return _patterns; }
            private set { _patterns = value; }
        }
        private List<PatternData> _patterns = new List<PatternData>();

        /// <summary>Gets or sets the Target value</summary>
        [DataMember]
        public string Target
        {
            get { return _target; }
            set { _target = value; }
        }
        private string _target;

        public Pattern Pattern
        {
            get
            {
                Pattern pattern = null;
                Pattern nextPattern = pattern;
                foreach (PatternData patternData in Patterns)
                {
                    if (nextPattern == null)
                    {
                        pattern = patternData.CreatePattern();
                        nextPattern = pattern;
                    }
                    else
                    {
                        nextPattern.NextPattern = patternData.CreatePattern();
                        nextPattern = nextPattern.NextPattern;
                    }
                }
                return pattern;
            }
        }

        public ShipData()
        {
        }

        public void AddPattern(Pattern nextPattern)
        {
            Patterns.Add(new PatternData(nextPattern));
        }

        public ShipData Duplicate()
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(ShipData));
            StringBuilder sb = new StringBuilder();
            StringWriter sw = new StringWriter(sb);
            XmlWriter xw = new XmlTextWriter(sw);
            serializer.WriteObject(xw, this);
            StringReader sr = new StringReader(sb.ToString());
            XmlReader xr = new XmlTextReader(sr);
            ShipData shipData = serializer.ReadObject(xr) as ShipData;
            return shipData;
        }
    }
}
